﻿using System;
using System.IO;

namespace ElectronicCommerce.Framework.Utility
{
    /// <summary>
    /// Directory Helper
    /// </summary>
    public class DirectoryHelper
    {
        public static void CopyDir(string sourceDir, string destDir)
        {
            CopyDir(sourceDir, destDir, "*.*", SearchOption.AllDirectories);
        }

        public static void CopyDir(string sourceDir, string destDir, bool selfFlag)
        {
            CopyDir(sourceDir, destDir, "*.*", SearchOption.AllDirectories, selfFlag);
        }

        public static void CopyDir(string sourceDir, string destDir, string searchPattern)
        {
            CopyDir(sourceDir, destDir, searchPattern, SearchOption.AllDirectories);
        }

        public static void CopyDir(string sourceDir, string destDir, SearchOption searchOption)
        {
            CopyDir(sourceDir, destDir, "*.*", searchOption);
        }

        public static void CopyDir(string sourceDir, string destDir, string searchPattern, SearchOption searchOption)
        {
            //check params
            if (string.IsNullOrEmpty(sourceDir)) return;
            if (string.IsNullOrEmpty(destDir)) return;
            if (!Directory.Exists(sourceDir)) return;

            //check destdir
            if (!Directory.Exists(destDir))
            {
                Directory.CreateDirectory(destDir);
            }

            string tempPath;
            string relativeFilePath;
            int sourceDirPathLength;

            sourceDirPathLength = sourceDir.Length;
            if (!sourceDir.EndsWith(@"\")) sourceDirPathLength = sourceDirPathLength + 1;

            //search all configuration file and copy to destdir
            string[] files = Directory.GetFiles(sourceDir, searchPattern, searchOption);
            foreach (string tempFileName in files)
            {
                relativeFilePath = tempFileName.Substring(sourceDirPathLength);
                tempPath = Path.Combine(destDir, relativeFilePath);

                relativeFilePath = Path.GetDirectoryName(tempPath);
                if (!Directory.Exists(relativeFilePath))
                {
                    Directory.CreateDirectory(relativeFilePath);
                }

                try
                {
                    if (File.Exists(tempPath)) File.SetAttributes(tempPath, FileAttributes.Normal);
                    File.Copy(tempFileName, tempPath, true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
        }

        public static void CopyDir(string sourceDir, string destDir, string searchPattern, SearchOption searchOption, bool selfFlag)
        {
            if (selfFlag)
            {
                string tempStr = sourceDir;
                if (tempStr.EndsWith(@"\")) tempStr = tempStr.Substring(0, tempStr.Length - 1);
                tempStr = tempStr.Substring(tempStr.LastIndexOf(@"\") + 1);
                tempStr = Path.Combine(destDir, tempStr);

                if (!Directory.Exists(tempStr))
                {
                    Directory.CreateDirectory(tempStr);
                }

                CopyDir(sourceDir, tempStr, searchPattern, searchOption);
            }
        }
    }
}
